﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using Microsoft.Win32;
using System.IO;
using ESRI.ArcGIS.DataSourcesFile;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.esriSystem;

namespace ZJGIS.Engine.Controls
{
    /// <summary>
    /// 打开或保存文件对话框
    /// </summary>
    public partial class OpenDataDialog : Office2007Form
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public OpenDataDialog()
        {
            InitializeComponent();
            dirTree1.FileNameText = txtFileName;
            dirTree1.UpwardButton = btnUp;
            dirTree1.FileList = fileList1;

            fileList1.BackButton = btnBack;
            fileList1.ForwardButton = btnForward;
            fileList1.DirTree = dirTree1;

            dirTree1.Initialize();

            SuperTooltip superTooltip1 = new SuperTooltip();
            superTooltip1.SetSuperTooltip(this.txtSearch,
                new SuperTooltipInfo("输入路径后按回车键进行搜索", "", "", null, null, eTooltipColor.Office2003));

            fileList1.DoubleClick += new EventHandler(fileList1_DoubleClick);

        }

        #region 属性
        /// <summary>
        /// 
        /// </summary>
        private const string REG_PATH = "SOFTWARE";
        /// <summary>
        /// 
        /// </summary>
        private const string KEY = "OpenDataCurrentPath";
        /// <summary>
        /// 
        /// </summary>
        private RegistryKey rk;
        /// <summary>
        /// 
        /// </summary>
        private List<IDataset> pDatasets;
        /// <summary>
        /// 
        /// </summary>
        private List<ILayer> pLayerFiles;
        /// <summary>
        /// 
        /// </summary>
        public EventHandler OnOkClick;
        /// <summary>
        /// CAD或DGN的预览图（因为一次只能选中一个预览图，所以不需要用集合）
        /// </summary>
        public ICadDrawingDataset CadDrawingDataset { get; private set; }
        /// <summary>
        /// 获取（第一个）数据的完整路径
        /// </summary>
        public string SelectPath
        {
            get
            {
                if (txtFileName.Text.Trim() == "") 
                    return "";
                return txtFileName.Text.Trim().Split('|')[0];
            }
        }

        /// <summary>
        /// 设置多选
        /// </summary>
        public bool Multiselect
        {
            set
            {
                fileList1.MultiSelect = value;
            }
        }
        /// <summary>
        /// 设置默认路径，若不设则取上一次打开数据的路径
        /// </summary>
        public string InitialDirectory { private get; set; }

        /// <summary>
        /// 
        /// </summary>
        private zjgisDialogType _DialogType = zjgisDialogType.Open;
        /// <summary>
        /// 
        /// </summary>
        [Browsable(false)]
        public zjgisDialogType DialogType
        {
            get { return _DialogType; }
            set
            {
                _DialogType = value;
                if (_DialogType == zjgisDialogType.Save)
                {
                    btnOK.Text = "确  定";
                    txtSearch.Visible = false;
                    lblSearch.Visible = false;
                    fileList1.MultiSelect = false;
                    this.Text = "保存数据";
                }
                else
                {
                    btnOK.Text = "加  载";
                    txtSearch.Visible = true;
                    lblSearch.Visible = true;
                    this.Text = "加载数据";
                }
            }
        }
        #endregion

        #region 构造函数
        
        #endregion

        #region 事件
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void fileList1_DoubleClick(object sender, EventArgs e)
        {
            if (fileList1.SelectedItems.Count != 1) return;

            ListViewItem lstvItem = fileList1.SelectedItems[0];
            if (lstvItem.Tag == null) return;

            object tag = lstvItem.Tag;

            //如果双击的是一个要素类、栅格数据集或者Layer节点、CAD节点，则调用OK按钮命令
            if (tag is FeatureClassNode || tag is RasterDatasetNode || tag is CadDrawingDatasetNode || tag is LayerNode)
            {
                btnOK_Click(null, null);
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnBack_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            fileList1.Backward();            
            this.Cursor = Cursors.Default;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnForward_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            fileList1.Forward();        
            this.Cursor = Cursors.Default;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnUp_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            dirTree1.Upward();        
            this.Cursor = Cursors.Default;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtSearch_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Return)
            {
                if (txtSearch.Text == "") return;
                Cursor = Cursors.WaitCursor;
                fileList1.ChangeDir(txtSearch.Text);
                Cursor = Cursors.Default;
                txtSearch.Text = "";
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FrmOpenData_Load(object sender, EventArgs e)
        {
            rk = Registry.LocalMachine.OpenSubKey(REG_PATH, true);

            if (InitialDirectory == null || InitialDirectory == "")
            {
                InitialDirectory = GetDefaultDir();
            }

            try
            {
                this.Cursor = Cursors.WaitCursor;
                fileList1.ChangeDir(InitialDirectory);

                InitialDirectory = "";//用过之后清空，以免影响下一次初始化路径
            }
            catch (Exception ex)
            {
                //ControlUtility.ShowErrorMsgbox(ex.Message);
            }
            finally
            {
                this.Cursor = Cursors.Default;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnOK_Click(object sender, EventArgs e)
        {
            if (txtFileName.Text.Trim() == "")
                return;
            string fileName = txtFileName.Text.Split('|')[0];
            string directory;
            if (System.IO.Path.GetDirectoryName(fileName) == null)
            {
                directory = fileName;
            }
            else
            {
                directory = fileName.Substring(0, fileName.LastIndexOf("\\"));
            }

            if (rk != null)
            {
                rk.SetValue(KEY, directory);
            }

            if (pDatasets != null)
                pDatasets.Clear();
            if (OnOkClick != null)
            {
                OnOkClick(null, null);
            }
            else
            {
                this.DialogResult = System.Windows.Forms.DialogResult.OK;
                this.Close();
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtFileName_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = true;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnRefresh_Click(object sender, EventArgs e)
        {
            this.dirTree1.Refresh();
        }

        #endregion

        /// <summary>
        /// 获取上一次打开数据的路径
        /// </summary>
        /// <returns></returns>
        private string GetDefaultDir()
        {
            if (rk == null)
            {
                return "C:\\";
            }
            else
            {
                if (rk.GetValue(KEY) == null)
                {
                    rk.SetValue(KEY, "C:\\");
                }
                return rk.GetValue(KEY).ToString();
            }
        }

        #region 获取DataNode和IDataset集合
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public List<DatasetNode> GetDatasets()
        {
            return fileList1.GetDatasets();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public List<FeatureClassNode> GetFeatureClasses()
        {
            return fileList1.GetFeatureClasses();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public List<RasterCatalogNode> GetRasterCatalogs()
        {
            return fileList1.GetRasterCatalogs();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public List<RasterDatasetNode> GetRasterDatasets()
        {
            return fileList1.GetRasterDatasets();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public List<CadDrawingDatasetNode> GetCadDrawingDataset()
        {
            return fileList1.GetCadDrawingDataset();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public List<LayerNode> GetLayerNodes()
        {
            return fileList1.GetLayerNodes();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public List<IDataNode> GetAllDataNodes()
        {
            List<IDataNode> pDataNodes = new List<IDataNode>();

            pDataNodes.AddRange(GetDatasets().ToArray());
            pDataNodes.AddRange(GetFeatureClasses().ToArray());
            pDataNodes.AddRange(GetRasterCatalogs().ToArray());
            pDataNodes.AddRange(GetRasterDatasets().ToArray());
            pDataNodes.AddRange(GetCadDrawingDataset().ToArray());
            pDataNodes.AddRange(GetLayerNodes().ToArray());

            //如果没有选中文件列表中的内容而是选中目录树
            if (pDataNodes.Count == 0 && this.dirTree1.SelectedNode != null)
            {
                object obj = this.dirTree1.SelectedNode.Tag;
                if (obj == null) return pDataNodes;

                if (obj is DataBaseNode)
                {
                    pDataNodes.Add(obj as IDataNode);
                }
                else if (obj is DatasetNode)
                {
                    pDataNodes.Add(obj as DatasetNode);
                }
                else if (obj is RasterCatalogNode)
                {
                    pDataNodes.Add(obj as RasterCatalogNode);
                }
                else if (obj is string)
                {
                    string path = obj as string;
                    DataBaseNode dbNode = new DataBaseNode();
                    dbNode.Path = path;
                    dbNode.WorkspaceType = WORKSPACETYPE.dir;
                    pDataNodes.Add(dbNode);
                }
            }
            return pDataNodes;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public List<IDataset> GetAllDatasets()
        {
            CadDrawingDataset = null;

            List<IDataset> pDatasets = new List<IDataset>();
            List<IDataNode> pDataNodes = GetAllDataNodes();
            if (pDataNodes != null)
            {
                pLayerFiles = null;
                foreach (IDataNode pDataNode in pDataNodes)
                {
                    try
                    {
                        if (pDataNode is CadDrawingDatasetNode)
                        {
                            CadDrawingDatasetNode cadDrawingDatasetNode = pDataNode as CadDrawingDatasetNode;
                            CadDrawingDataset = cadDrawingDatasetNode.OpenAsCadDrawingDatasetNode();
                        }
                        else if (pDataNode is LayerNode)
                        {
                            if (pLayerFiles == null) pLayerFiles = new List<ILayer>();
                            LayerNode layerNode = pDataNode as LayerNode;
                            ILayer pLayer = layerNode.OpenAsLayerNode();
                            pLayerFiles.Add(pLayer);
                        }
                        else
                        {
                            IDataset pDataset;

                            if (pDataNode.Tag != null && pDataNode.Tag is IDataset)
                            {
                                pDataset = pDataNode.Tag as IDataset;
                            }
                            else
                            {
                                pDataset = pDataNode.Open();
                            }
                            if (pDataset != null) pDatasets.Add(pDataset);
                        }
                    }
                    catch
                    {
                    }
                }
            }
            return pDatasets;
        }

        #endregion

        #region 获取ILayer集合
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public List<ILayer> GetAnnoLyrs()
        {
            List<ILayer> pLayers = new List<ILayer>();
            if (pDatasets == null || pDatasets.Count == 0) pDatasets = GetAllDatasets();

            foreach (IDataset pDataset in pDatasets)
            {
                if (pDataset == null) continue;

                if (pDataset.Type == esriDatasetType.esriDTFeatureDataset)
                {
                    //如果是CAD图层，则通过组的方式加载，调用GetCadGroupLyrs函数
                    if (pDataset.Workspace.WorkspaceFactory is CadWorkspaceFactory) continue;

                    IEnumDataset pEnumDataset = pDataset.Subsets;
                    IDataset pDatasetSub = pEnumDataset.Next();
                    while (pDatasetSub != null)
                    {
                        IFeatureClass pFeatureClass = pDatasetSub as IFeatureClass;
                        IFeatureLayer pFeatureLayer = null;
                        if (pFeatureClass.FeatureType == esriFeatureType.esriFTAnnotation)
                        {
                            pFeatureLayer = new FDOGraphicsLayerClass();
                            pFeatureLayer.Name = pDataset.Name;
                            //if (SystemConfig.systemConfigXml.基本信息.加载数据 == null ||
                            //    SystemConfig.systemConfigXml.基本信息.加载数据 == 0)
                            //{
                            //    pFeatureLayer.Name = pFeatureClass.AliasName;
                            //}
                            //else
                            //{
                                
                            //}

                            pFeatureLayer.FeatureClass = pFeatureClass;
                            pLayers.Add(pFeatureLayer);
                        }
                        pDatasetSub = pEnumDataset.Next();
                    }
                }
                else if (pDataset.Type == esriDatasetType.esriDTFeatureClass)
                {
                    IFeatureClass pFeatureClass = pDataset as IFeatureClass;
                    IFeatureLayer pFeatureLayer = null;
                    if (pFeatureClass == null) continue;

                    if (pFeatureClass.FeatureType == esriFeatureType.esriFTCoverageAnnotation)
                    {
                        IWorkspaceFactory pCadWKSFact = new CadWorkspaceFactory();

                        IFeatureWorkspace pWorkspace = pCadWKSFact.OpenFromFile(pDataset.Workspace.PathName, 0) as IFeatureWorkspace;
                        IFeatureDataset pFeatureDataset = pWorkspace.OpenFeatureDataset(pFeatureClass.FeatureDataset.Name);

                        IFeatureClassContainer pFeatureClassContainer = pFeatureDataset as IFeatureClassContainer;

                        IFeatureClass pFeatCls;
                        IEnumFeatureClass pEnumFeatureClass = pFeatureClassContainer.Classes;
                        pEnumFeatureClass.Reset();
                        pFeatCls = pEnumFeatureClass.Next();
                        IFeatureLayer pCadFeatureLayer;
                        IFeatureLayer pAnnoLyr = null;

                        //只有通过这种方式，得到的CAD图层才有符号信息，如果直接通过名称去找(pFeatureClassContainer.get_ClassByName)同样没有符号信息
                        while (pFeatCls != null)
                        {
                            if (pFeatCls.FeatureType == esriFeatureType.esriFTCoverageAnnotation)
                            {
                                pCadFeatureLayer = new CadAnnotationLayerClass();
                                pAnnoLyr = pCadFeatureLayer as IFeatureLayer;

                                pCadFeatureLayer.Name = pFeatureClass.FeatureDataset.Name + ":" + pDataset.Name;
                                pCadFeatureLayer.FeatureClass = pFeatCls;

                                break;
                            }

                            pFeatCls = pEnumFeatureClass.Next();
                        }

                        pLayers.Add(pAnnoLyr);
                    }
                    else if (pFeatureClass.FeatureType == esriFeatureType.esriFTAnnotation)
                    {
                        pFeatureLayer = new FDOGraphicsLayerClass();

                        if (pDataset.Workspace.WorkspaceFactory is CadWorkspaceFactory)
                        {
                            pFeatureLayer.Name = pFeatureClass.FeatureDataset.Name + ":" + pDataset.Name;
                        }
                        else
                        {
                            pFeatureLayer.Name = pDataset.Name;

                            //if (SystemConfig.systemConfigXml.基本信息.加载数据 == null ||
                            //    SystemConfig.systemConfigXml.基本信息.加载数据 == 0)
                            //{
                            //    pFeatureLayer.Name = pFeatureClass.AliasName;
                            //}
                            //else
                            //{
                                
                            //}
                        }
                        pFeatureLayer.FeatureClass = pFeatureClass;
                        pLayers.Add(pFeatureLayer);
                    }
                }
                
            }

            return pLayers;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public List<ILayer> GetPointLyrs()
        {
            List<ILayer> pLayers = new List<ILayer>();
            if (pDatasets == null || pDatasets.Count == 0) pDatasets = GetAllDatasets();

            foreach (IDataset pDataset in pDatasets)
            {
                if (pDataset == null) continue;

                if (pDataset.Type == esriDatasetType.esriDTFeatureDataset)
                {
                    if (pDataset.Workspace.WorkspaceFactory is CadWorkspaceFactory) continue;

                    IEnumDataset pEnumDataset = pDataset.Subsets;
                    IDataset pDatasetSub = pEnumDataset.Next();
                    while (pDatasetSub != null)
                    {
                        IFeatureClass pFeatureClass = pDatasetSub as IFeatureClass;
                        IFeatureLayer pFeatureLayer = null;
                        if (pFeatureClass.FeatureType == esriFeatureType.esriFTSimple &&
                            pFeatureClass.ShapeType == esriGeometryType.esriGeometryPoint)
                        {
                            pFeatureLayer = new FeatureLayerClass();

                            //if (SystemConfig.systemConfigXml.基本信息.加载数据 == null ||
                            //    SystemConfig.systemConfigXml.基本信息.加载数据 == 0)
                            //{
                            //    pFeatureLayer.Name = pFeatureClass.AliasName;
                            //}
                            //else
                            //{
                                
                            //}
                            pFeatureLayer.Name = pDataset.Name;
                            pFeatureLayer.FeatureClass = pFeatureClass;
                            pLayers.Add(pFeatureLayer);
                        }
                        pDatasetSub = pEnumDataset.Next();
                    }
                }
                else if (pDataset.Type == esriDatasetType.esriDTFeatureClass)
                {
                    IFeatureClass pFeatureClass = pDataset as IFeatureClass;
                    if (pFeatureClass == null) continue;
                    if (pFeatureClass.FeatureType != esriFeatureType.esriFTSimple) continue;

                    IFeatureLayer pFeatureLayer = null;

                    if (pDataset.Workspace.WorkspaceFactory is CadWorkspaceFactory)
                    {
                        if (pDataset.Name != "Point") continue;

                        IWorkspaceFactory pCadWKSFact = new CadWorkspaceFactory();

                        IFeatureWorkspace pWorkspace = pCadWKSFact.OpenFromFile(pDataset.Workspace.PathName, 0) as IFeatureWorkspace;
                        IFeatureDataset pFeatureDataset = pWorkspace.OpenFeatureDataset(pFeatureClass.FeatureDataset.Name);

                        IFeatureClassContainer pFeatureClassContainer = pFeatureDataset as IFeatureClassContainer;

                        IFeatureClass pFeatCls;
                        IEnumFeatureClass pEnumFeatureClass = pFeatureClassContainer.Classes;
                        pEnumFeatureClass.Reset();
                        pFeatCls = pEnumFeatureClass.Next();
                        IFeatureLayer pCadFeatureLayer;

                        IFeatureLayer pAnnoLyr = null;
                        IFeatureLayer pPointLyr = null;
                        IFeatureLayer pPolylineLyr = null;
                        IFeatureLayer pPolygonLyr = null;
                        IFeatureLayer pMutiPatchLyr = null;

                        //只有通过这种方式，得到的CAD图层才有符号信息，如果直接通过名称去找(pFeatureClassContainer.get_ClassByName)同样没有符号信息
                        while (pFeatCls != null)
                        {
                            if (pFeatCls.FeatureType == esriFeatureType.esriFTCoverageAnnotation)
                            {
                                pCadFeatureLayer = new CadAnnotationLayerClass();
                                pAnnoLyr = pCadFeatureLayer as IFeatureLayer;
                            }
                            else
                            {
                                pCadFeatureLayer = new CadFeatureLayerClass();

                                switch (pFeatCls.ShapeType)
                                {
                                    case esriGeometryType.esriGeometryPoint:
                                        pPointLyr = pCadFeatureLayer as IFeatureLayer;
                                        break;
                                    case esriGeometryType.esriGeometryPolyline:
                                        pPolylineLyr = pCadFeatureLayer as IFeatureLayer;

                                        break;
                                    case esriGeometryType.esriGeometryPolygon:
                                        pPolygonLyr = pCadFeatureLayer as IFeatureLayer;
                                        break;
                                    case esriGeometryType.esriGeometryMultiPatch:
                                        pMutiPatchLyr = pCadFeatureLayer as IFeatureLayer;
                                        break;
                                    default:
                                        goto NEXTFEATCLS;
                                }
                            }

                            pCadFeatureLayer.Name = pFeatureClass.FeatureDataset.Name + ":" + pDataset.Name;
                            pCadFeatureLayer.FeatureClass = pFeatCls;

                        NEXTFEATCLS:
                            pFeatCls = pEnumFeatureClass.Next();
                        }

                        if (pPointLyr != null) pLayers.Add(pPointLyr);

                    }
                    else
                    {
                        if (pFeatureClass.FeatureType == esriFeatureType.esriFTSimple &&
                            pFeatureClass.ShapeType == esriGeometryType.esriGeometryPoint)
                        {
                            pFeatureLayer = new FeatureLayerClass();
                            pFeatureLayer.Name = pDataset.Name;
                            //if (SystemConfig.systemConfigXml.基本信息.加载数据 == null ||
                            //    SystemConfig.systemConfigXml.基本信息.加载数据 == 0)
                            //{
                            //    pFeatureLayer.Name = pFeatureClass.AliasName;
                            //}
                            //else
                            //{
                                
                            //}                            
                            pFeatureLayer.FeatureClass = pFeatureClass;
                            pLayers.Add(pFeatureLayer);
                        }
                    }
                }
            }

            return pLayers;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public List<ILayer> GetPolylineLyrs()
        {
            List<ILayer> pLayers = new List<ILayer>();
            if (pDatasets == null || pDatasets.Count == 0) pDatasets = GetAllDatasets();

            foreach (IDataset pDataset in pDatasets)
            {
                if (pDataset == null) continue;

                if (pDataset.Type == esriDatasetType.esriDTFeatureDataset)
                {
                    if (pDataset.Workspace.WorkspaceFactory is CadWorkspaceFactory) continue;

                    IEnumDataset pEnumDataset = pDataset.Subsets;
                    IDataset pDatasetSub = pEnumDataset.Next();
                    while (pDatasetSub != null)
                    {
                        IFeatureClass pFeatureClass = pDatasetSub as IFeatureClass;
                        IFeatureLayer pFeatureLayer = null;
                        if (pFeatureClass.FeatureType == esriFeatureType.esriFTSimple &&
                            pFeatureClass.ShapeType == esriGeometryType.esriGeometryPolyline)
                        {
                            pFeatureLayer = new FeatureLayerClass();
                            pFeatureLayer.Name = pDataset.Name;
                            //if (SystemConfig.systemConfigXml.基本信息.加载数据 == null ||
                            //    SystemConfig.systemConfigXml.基本信息.加载数据 == 0)
                            //{
                            //    pFeatureLayer.Name = pFeatureClass.AliasName;
                            //}
                            //else
                            //{
                                
                            //}
                            pFeatureLayer.FeatureClass = pFeatureClass;
                            pLayers.Add(pFeatureLayer);
                        }
                        pDatasetSub = pEnumDataset.Next();
                    }
                }
                else if (pDataset.Type == esriDatasetType.esriDTFeatureClass)
                {
                    IFeatureClass pFeatureClass = pDataset as IFeatureClass;
                    IFeatureLayer pFeatureLayer = null;
                    if (pFeatureClass == null) continue;

                    if (pFeatureClass.FeatureType != esriFeatureType.esriFTSimple) continue;

                    if (pDataset.Workspace.WorkspaceFactory is CadWorkspaceFactory)
                    {
                        if (pDataset.Name != "Polyline") continue;

                        IWorkspaceFactory pCadWKSFact = new CadWorkspaceFactory();

                        IFeatureWorkspace pWorkspace = pCadWKSFact.OpenFromFile(pDataset.Workspace.PathName, 0) as IFeatureWorkspace;
                        IFeatureDataset pFeatureDataset = pWorkspace.OpenFeatureDataset(pFeatureClass.FeatureDataset.Name);

                        IFeatureClassContainer pFeatureClassContainer = pFeatureDataset as IFeatureClassContainer;

                        IFeatureClass pFeatCls;
                        IEnumFeatureClass pEnumFeatureClass = pFeatureClassContainer.Classes;
                        pEnumFeatureClass.Reset();
                        pFeatCls = pEnumFeatureClass.Next();
                        IFeatureLayer pCadFeatureLayer;

                        IFeatureLayer pAnnoLyr = null;
                        IFeatureLayer pPointLyr = null;
                        IFeatureLayer pPolylineLyr = null;
                        IFeatureLayer pPolygonLyr = null;
                        IFeatureLayer pMutiPatchLyr = null;

                        //只有通过这种方式，得到的CAD图层才有符号信息，如果直接通过名称去找(pFeatureClassContainer.get_ClassByName)同样没有符号信息
                        while (pFeatCls != null)
                        {
                            if (pFeatCls.FeatureType == esriFeatureType.esriFTCoverageAnnotation)
                            {
                                pCadFeatureLayer = new CadAnnotationLayerClass();
                                pAnnoLyr = pCadFeatureLayer as IFeatureLayer;
                            }
                            else
                            {
                                pCadFeatureLayer = new CadFeatureLayerClass();

                                switch (pFeatCls.ShapeType)
                                {
                                    case esriGeometryType.esriGeometryPoint:
                                        pPointLyr = pCadFeatureLayer as IFeatureLayer;
                                        break;
                                    case esriGeometryType.esriGeometryPolyline:
                                        pPolylineLyr = pCadFeatureLayer as IFeatureLayer;

                                        break;
                                    case esriGeometryType.esriGeometryPolygon:
                                        pPolygonLyr = pCadFeatureLayer as IFeatureLayer;
                                        break;
                                    case esriGeometryType.esriGeometryMultiPatch:
                                        pMutiPatchLyr = pCadFeatureLayer as IFeatureLayer;
                                        break;
                                    default:
                                        goto NEXTFEATCLS;
                                }
                            }

                            pCadFeatureLayer.Name = pFeatureClass.FeatureDataset.Name + ":" + pDataset.Name;
                            pCadFeatureLayer.FeatureClass = pFeatCls;

                        NEXTFEATCLS:
                            pFeatCls = pEnumFeatureClass.Next();
                        }

                        if (pPolylineLyr != null) pLayers.Add(pPolylineLyr);

                    }
                    else
                    {
                        if (pFeatureClass.FeatureType == esriFeatureType.esriFTSimple &&
                            pFeatureClass.ShapeType == esriGeometryType.esriGeometryPolyline)
                        {
                            pFeatureLayer = new FeatureLayerClass();

                            if (pDataset.Workspace.WorkspaceFactory is CadWorkspaceFactory)
                            {
                                pFeatureLayer.Name = pFeatureClass.FeatureDataset.Name + ":" + pDataset.Name;
                            }
                            else
                            {
                                pFeatureLayer.Name = pDataset.Name;
                                //if (SystemConfig.systemConfigXml.基本信息.加载数据 == null ||
                                //    SystemConfig.systemConfigXml.基本信息.加载数据 == 0)
                                //{
                                //    pFeatureLayer.Name = pFeatureClass.AliasName;
                                //}
                                //else
                                //{
                                    
                                //}
                            }

                            pFeatureLayer.FeatureClass = pFeatureClass;
                            pLayers.Add(pFeatureLayer);
                        }
                    }
                }
            }

            return pLayers;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public List<ILayer> GetPolygonLyrs()
        {
            List<ILayer> pLayers = new List<ILayer>();
            if (pDatasets == null || pDatasets.Count == 0) pDatasets = GetAllDatasets();

            foreach (IDataset pDataset in pDatasets)
            {
                if (pDataset == null) continue;

                if (pDataset.Type == esriDatasetType.esriDTFeatureDataset)
                {
                    if (pDataset.Workspace.WorkspaceFactory is CadWorkspaceFactory) continue;

                    IEnumDataset pEnumDataset = pDataset.Subsets;
                    IDataset pDatasetSub = pEnumDataset.Next();
                    while (pDatasetSub != null)
                    {
                        IFeatureClass pFeatureClass = pDatasetSub as IFeatureClass;
                        IFeatureLayer pFeatureLayer = null;
                        if (pFeatureClass.FeatureType == esriFeatureType.esriFTSimple &&
                            pFeatureClass.ShapeType == esriGeometryType.esriGeometryPolygon)
                        {
                            pFeatureLayer = new FeatureLayerClass();
                            pFeatureLayer.Name = pDataset.Name;
                            //if (SystemConfig.systemConfigXml.基本信息.加载数据 == null ||
                            //    SystemConfig.systemConfigXml.基本信息.加载数据 == 0)
                            //{
                            //    pFeatureLayer.Name = pFeatureClass.AliasName;
                            //}
                            //else
                            //{
                                
                            //}

                            pFeatureLayer.FeatureClass = pFeatureClass;
                            pLayers.Add(pFeatureLayer);
                        }
                        pDatasetSub = pEnumDataset.Next();
                    }
                }
                else if (pDataset.Type == esriDatasetType.esriDTFeatureClass)
                {
                    IFeatureClass pFeatureClass = pDataset as IFeatureClass;
                    IFeatureLayer pFeatureLayer = null;
                    if (pFeatureClass == null) continue;

                    if (pFeatureClass.FeatureType != esriFeatureType.esriFTSimple) continue;

                    if (pDataset.Workspace.WorkspaceFactory is CadWorkspaceFactory)
                    {
                        if (pDataset.Name != "Polygon") continue;

                        IWorkspaceFactory pCadWKSFact = new CadWorkspaceFactory();

                        IFeatureWorkspace pWorkspace = pCadWKSFact.OpenFromFile(pDataset.Workspace.PathName, 0) as IFeatureWorkspace;
                        IFeatureDataset pFeatureDataset = pWorkspace.OpenFeatureDataset(pFeatureClass.FeatureDataset.Name);

                        IFeatureClassContainer pFeatureClassContainer = pFeatureDataset as IFeatureClassContainer;

                        IFeatureClass pFeatCls;
                        IEnumFeatureClass pEnumFeatureClass = pFeatureClassContainer.Classes;
                        pEnumFeatureClass.Reset();
                        pFeatCls = pEnumFeatureClass.Next();
                        IFeatureLayer pCadFeatureLayer;

                        IFeatureLayer pAnnoLyr = null;
                        IFeatureLayer pPointLyr = null;
                        IFeatureLayer pPolylineLyr = null;
                        IFeatureLayer pPolygonLyr = null;
                        IFeatureLayer pMutiPatchLyr = null;

                        //只有通过这种方式，得到的CAD图层才有符号信息，如果直接通过名称去找(pFeatureClassContainer.get_ClassByName)同样没有符号信息
                        while (pFeatCls != null)
                        {
                            if (pFeatCls.FeatureType == esriFeatureType.esriFTCoverageAnnotation)
                            {
                                pCadFeatureLayer = new CadAnnotationLayerClass();
                                pAnnoLyr = pCadFeatureLayer as IFeatureLayer;
                            }
                            else
                            {
                                pCadFeatureLayer = new CadFeatureLayerClass();

                                switch (pFeatCls.ShapeType)
                                {
                                    case esriGeometryType.esriGeometryPoint:
                                        pPointLyr = pCadFeatureLayer as IFeatureLayer;
                                        break;
                                    case esriGeometryType.esriGeometryPolyline:
                                        pPolylineLyr = pCadFeatureLayer as IFeatureLayer;

                                        break;
                                    case esriGeometryType.esriGeometryPolygon:
                                        pPolygonLyr = pCadFeatureLayer as IFeatureLayer;
                                        break;
                                    case esriGeometryType.esriGeometryMultiPatch:
                                        pMutiPatchLyr = pCadFeatureLayer as IFeatureLayer;
                                        break;
                                    default:
                                        goto NEXTFEATCLS;
                                }
                            }

                            pCadFeatureLayer.Name = pFeatureClass.FeatureDataset.Name + ":" + pDataset.Name;
                            pCadFeatureLayer.FeatureClass = pFeatCls;

                        NEXTFEATCLS:
                            pFeatCls = pEnumFeatureClass.Next();
                        }

                        if (pPolygonLyr != null) pLayers.Add(pPolygonLyr);

                    }
                    else
                    {
                        if (pFeatureClass.FeatureType == esriFeatureType.esriFTSimple &&
                            pFeatureClass.ShapeType == esriGeometryType.esriGeometryPolygon)
                        {
                            pFeatureLayer = new FeatureLayerClass();

                            if (pDataset.Workspace.WorkspaceFactory is CadWorkspaceFactory)
                            {
                                pFeatureLayer.Name = pFeatureClass.FeatureDataset.Name + ":" + pDataset.Name;
                            }
                            else
                            {
                                pFeatureLayer.Name = pDataset.Name;
                                //if (SystemConfig.systemConfigXml.基本信息.加载数据 == null ||
                                //    SystemConfig.systemConfigXml.基本信息.加载数据 == 0)
                                //{
                                //    pFeatureLayer.Name = pFeatureClass.AliasName;
                                //}
                                //else
                                //{
                                    
                                //}
                            }

                            pFeatureLayer.FeatureClass = pFeatureClass;
                            pLayers.Add(pFeatureLayer);
                        }
                    }
                }
            }

            return pLayers;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public List<ILayer> GetMutiPatchLyrs()
        {
            List<ILayer> pLayers = new List<ILayer>();
            if (pDatasets == null || pDatasets.Count == 0) pDatasets = GetAllDatasets();

            foreach (IDataset pDataset in pDatasets)
            {
                if (pDataset == null) continue;

                if (pDataset.Type == esriDatasetType.esriDTFeatureDataset)
                {
                    if (pDataset.Workspace.WorkspaceFactory is CadWorkspaceFactory) continue;

                    IEnumDataset pEnumDataset = pDataset.Subsets;
                    IDataset pDatasetSub = pEnumDataset.Next();
                    while (pDatasetSub != null)
                    {
                        IFeatureClass pFeatureClass = pDatasetSub as IFeatureClass;
                        IFeatureLayer pFeatureLayer = null;
                        if (pFeatureClass.FeatureType == esriFeatureType.esriFTSimple &&
                            pFeatureClass.ShapeType == esriGeometryType.esriGeometryMultiPatch)
                        {
                            pFeatureLayer = new FeatureLayerClass();

                            //if (SystemConfig.systemConfigXml.基本信息.加载数据 == null ||
                            //    SystemConfig.systemConfigXml.基本信息.加载数据 == 0)
                            //{
                            //    pFeatureLayer.Name = pFeatureClass.AliasName;
                            //}
                            //else
                            {
                                pFeatureLayer.Name = pDataset.Name;
                            }

                            pFeatureLayer.FeatureClass = pFeatureClass;
                            pLayers.Add(pFeatureLayer);
                        }
                        pDatasetSub = pEnumDataset.Next();
                    }
                }
                else if (pDataset.Type == esriDatasetType.esriDTFeatureClass)
                {
                    IFeatureClass pFeatureClass = pDataset as IFeatureClass;
                    IFeatureLayer pFeatureLayer = null;
                    if (pFeatureClass == null) continue;

                    if (pFeatureClass.FeatureType != esriFeatureType.esriFTSimple) continue;

                    if (pDataset.Workspace.WorkspaceFactory is CadWorkspaceFactory)
                    {
                        if (pDataset.Name != "MultiPatch") continue;

                        IWorkspaceFactory pCadWKSFact = new CadWorkspaceFactory();

                        IFeatureWorkspace pWorkspace = pCadWKSFact.OpenFromFile(pDataset.Workspace.PathName, 0) as IFeatureWorkspace;
                        IFeatureDataset pFeatureDataset = pWorkspace.OpenFeatureDataset(pFeatureClass.FeatureDataset.Name);

                        IFeatureClassContainer pFeatureClassContainer = pFeatureDataset as IFeatureClassContainer;

                        IFeatureClass pFeatCls;
                        IEnumFeatureClass pEnumFeatureClass = pFeatureClassContainer.Classes;
                        pEnumFeatureClass.Reset();
                        pFeatCls = pEnumFeatureClass.Next();
                        IFeatureLayer pCadFeatureLayer;

                        IFeatureLayer pAnnoLyr = null;
                        IFeatureLayer pPointLyr = null;
                        IFeatureLayer pPolylineLyr = null;
                        IFeatureLayer pPolygonLyr = null;
                        IFeatureLayer pMutiPatchLyr = null;

                        //只有通过这种方式，得到的CAD图层才有符号信息，如果直接通过名称去找(pFeatureClassContainer.get_ClassByName)同样没有符号信息
                        while (pFeatCls != null)
                        {
                            if (pFeatCls.FeatureType == esriFeatureType.esriFTCoverageAnnotation)
                            {
                                pCadFeatureLayer = new CadAnnotationLayerClass();
                                pAnnoLyr = pCadFeatureLayer as IFeatureLayer;
                            }
                            else
                            {
                                pCadFeatureLayer = new CadFeatureLayerClass();

                                switch (pFeatCls.ShapeType)
                                {
                                    case esriGeometryType.esriGeometryPoint:
                                        pPointLyr = pCadFeatureLayer as IFeatureLayer;
                                        break;
                                    case esriGeometryType.esriGeometryPolyline:
                                        pPolylineLyr = pCadFeatureLayer as IFeatureLayer;

                                        break;
                                    case esriGeometryType.esriGeometryPolygon:
                                        pPolygonLyr = pCadFeatureLayer as IFeatureLayer;
                                        break;
                                    case esriGeometryType.esriGeometryMultiPatch:
                                        pMutiPatchLyr = pCadFeatureLayer as IFeatureLayer;
                                        break;
                                    default:
                                        goto NEXTFEATCLS;
                                }
                            }

                            pCadFeatureLayer.Name = pFeatureClass.FeatureDataset.Name + ":" + pDataset.Name;
                            pCadFeatureLayer.FeatureClass = pFeatCls;

                        NEXTFEATCLS:
                            pFeatCls = pEnumFeatureClass.Next();
                        }

                        if (pMutiPatchLyr != null) pLayers.Add(pMutiPatchLyr);

                    }
                    else
                    {
                        if (pFeatureClass.FeatureType == esriFeatureType.esriFTSimple &&
                            pFeatureClass.ShapeType == esriGeometryType.esriGeometryMultiPatch)
                        {
                            pFeatureLayer = new FeatureLayerClass();

                            if (pDataset.Workspace.WorkspaceFactory is CadWorkspaceFactory)
                            {
                                pFeatureLayer.Name = pFeatureClass.FeatureDataset.Name + ":" + pDataset.Name;
                            }
                            else
                            {

                                //if (SystemConfig.systemConfigXml.基本信息.加载数据 == null ||
                                //    SystemConfig.systemConfigXml.基本信息.加载数据 == 0)
                                //{
                                //    pFeatureLayer.Name = pFeatureClass.AliasName;
                                //}
                                //else
                                {
                                    pFeatureLayer.Name = pDataset.Name;
                                }
                            }
                            pFeatureLayer.FeatureClass = pFeatureClass;
                            pLayers.Add(pFeatureLayer);
                        }
                    }
                }
            }

            return pLayers;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public List<ILayer> GetRasterDatasetLyrs()
        {
            List<ILayer> pLayers = new List<ILayer>();
            if (pDatasets == null || pDatasets.Count == 0) pDatasets = GetAllDatasets();

            foreach (IDataset pDataset in pDatasets)
            {
                if (pDataset == null) continue;

                if (pDataset.Type == esriDatasetType.esriDTRasterDataset)
                {
                    IRasterLayer pRasterLayer = new RasterLayerClass();
                    pRasterLayer.CreateFromDataset(pDataset as IRasterDataset);
                    pLayers.Add(pRasterLayer);
                }
            }
            return pLayers;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public List<ILayer> GetRasterCatalogLyrs()
        {
            List<ILayer> pLayers = new List<ILayer>();
            if (pDatasets == null || pDatasets.Count == 0) pDatasets = GetAllDatasets();

            foreach (IDataset pDataset in pDatasets)
            {
                if (pDataset == null) continue;

                if (pDataset.Type == esriDatasetType.esriDTRasterCatalog)
                {
                    IGdbRasterCatalogLayer pGdbRasterCatalogLayer = new GdbRasterCatalogLayerClass();
                    if (pGdbRasterCatalogLayer.Setup(pDataset as ITable))
                    {
                        pLayers.Add(pGdbRasterCatalogLayer as ILayer);
                    }
                }
            }

            return pLayers;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public List<ILayer> GetLayers()
        {
            if (pDatasets == null || pDatasets.Count == 0) pDatasets = GetAllDatasets();
            if (pLayerFiles == null) pLayerFiles = new List<ILayer>();

            return pLayerFiles;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public ILayer GetCadDrawingLyrs()
        {
            if (pDatasets == null || pDatasets.Count == 0) pDatasets = GetAllDatasets();

            if (CadDrawingDataset == null) return null;

            ICadLayer pCadLayer = new CadLayerClass();
            pCadLayer.CadDrawingDataset = CadDrawingDataset;
            pCadLayer.Name = System.IO.Path.GetFileName(CadDrawingDataset.FilePath);

            return pCadLayer;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public List<ILayer> GetCadGroupLyrs()
        {
            List<ILayer> pLayers = new List<ILayer>();
            if (pDatasets == null || pDatasets.Count == 0) pDatasets = GetAllDatasets();

            foreach (IDataset pDataset in pDatasets)
            {
                if (pDataset == null) continue;

                if (pDataset.Type ==esriDatasetType.esriDTFeatureDataset &&
                    pDataset.Workspace.WorkspaceFactory is CadWorkspaceFactory)
                {
                    IGroupLayer pGropLayer = new GroupLayerClass();

                    IWorkspaceFactory pCadWKSFact = new CadWorkspaceFactory();

                    IFeatureWorkspace pWorkspace = pCadWKSFact.OpenFromFile(pDataset.Workspace.PathName, 0) as IFeatureWorkspace;
                    IFeatureDataset pFeatureDataset = pWorkspace.OpenFeatureDataset(pDataset.BrowseName);
                    IFeatureClassContainer pFeatureClassContainer = pFeatureDataset as IFeatureClassContainer;

                    IFeatureClass pFeatCls;
                    IEnumFeatureClass pEnumFeatureClass = pFeatureClassContainer.Classes;
                    pEnumFeatureClass.Reset();
                    pFeatCls = pEnumFeatureClass.Next();
                    IFeatureLayer pCadFeatureLayer;

                    IFeatureLayer pAnnoLyr = null;
                    IFeatureLayer pPointLyr = null;
                    IFeatureLayer pPolylineLyr = null;
                    IFeatureLayer pPolygonLyr = null;
                    IFeatureLayer pMutiPatchLyr = null;

                    while (pFeatCls != null)
                    {
                        if (pFeatCls.FeatureType == esriFeatureType.esriFTCoverageAnnotation)
                        {
                            pCadFeatureLayer = new CadAnnotationLayerClass();
                            pCadFeatureLayer.Name = pDataset.Name + ":Annotation";
                            pAnnoLyr = pCadFeatureLayer as IFeatureLayer;
                        }
                        else
                        {
                            string lyrname = "";
                            pCadFeatureLayer = new CadFeatureLayerClass();

                            switch (pFeatCls.ShapeType)
                            {
                                case esriGeometryType.esriGeometryPoint:
                                    lyrname = pDataset.Name + ":Point";
                                    pPointLyr = pCadFeatureLayer as IFeatureLayer;
                                    break;
                                case esriGeometryType.esriGeometryPolyline:
                                    lyrname = pDataset.Name + ":Polyline";
                                    pPolylineLyr = pCadFeatureLayer as IFeatureLayer;

                                    break;
                                case esriGeometryType.esriGeometryPolygon:
                                    lyrname = pDataset.Name + ":Polygon";
                                    pPolygonLyr = pCadFeatureLayer as IFeatureLayer;
                                    break;
                                case esriGeometryType.esriGeometryMultiPatch:
                                    lyrname = pDataset.Name + ":MultiPatch";
                                    pMutiPatchLyr = pCadFeatureLayer as IFeatureLayer;
                                    break;
                                default:
                                    goto NEXTFEATCLS;
                            }

                            pCadFeatureLayer.Name = lyrname;
                        }

                        pCadFeatureLayer.FeatureClass = pFeatCls;

                    NEXTFEATCLS:
                        pFeatCls = pEnumFeatureClass.Next();
                    }

                    pGropLayer.Add(pAnnoLyr);
                    pGropLayer.Add(pPointLyr);
                    pGropLayer.Add(pPolylineLyr);
                    pGropLayer.Add(pPolygonLyr);
                    pGropLayer.Add(pMutiPatchLyr);

                    pGropLayer.Expanded = false;

                    pGropLayer.Name = pDataset.Name + " Gruop Layer";

                    pLayers.Add(pGropLayer as ILayer);
                }
            }

            return pLayers;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public List<ILayer> GetAllLayers()
        {
            List<ILayer> pLayers = new List<ILayer>();

            pLayers.AddRange(GetCadGroupLyrs());
            pLayers.AddRange(GetRasterCatalogLyrs());
            pLayers.AddRange(GetRasterDatasetLyrs());
            pLayers.AddRange(GetMutiPatchLyrs());
            pLayers.AddRange(GetPolygonLyrs());
            pLayers.AddRange(GetPolylineLyrs());
            pLayers.AddRange(GetPointLyrs());
            pLayers.AddRange(GetAnnoLyrs());
            pLayers.AddRange(GetLayers());

            ILayer pLayer = GetCadDrawingLyrs();
            if (pLayer != null) pLayers.Add(pLayer);

            return pLayers;
        }

        #endregion

        /// <summary>
        /// 
        /// </summary>
        public enum zjgisDialogType
        {
            /// <summary>
            /// 打开文件
            /// </summary>
            Open,
            /// <summary>
            /// 保存文件
            /// </summary>
            Save
        }


    }
}
